package com.tarena.sentinel.study.demo02.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tarena.sentinel.study.demo02.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    private HelloService helloService;
    /**
     * 接收测试请求的接口功能
     * http://localhost:8080/hello?name=王翠花
     * hello sentinel i am 王翠花
     */
    @GetMapping("/hello")
    public String sayHi(String name) {
        Entry entry=null;
        String result = null;
        try{
            entry=SphU.entry("sayHi");
            result = helloService.sayHi(name);
        }catch (BlockException e){
            log.info("限流了",e);
            return "当前限流规则生效 无法访问资源";
        }finally {
            if (entry!=null){
                entry.exit();
            }
        }
        return result;
    }
}













